package com.lecyon.farm.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lecyon.farm.base.BaseResponse;
import com.lecyon.farm.base.RestResponse;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.vo.base.BaseViewVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @author YaoZheng
 */
@Component
public class ResultUtil {

    /**
     * 生成成功 BaseResponse
     *
     * @return
     */
    public static BaseResponse success() {
        BaseResponse result = new BaseResponse();
        result.setSuccess(true);
        result.setCode(CommonConstant.SUCCESS);
        result.setErrorMessage("success");
        result.setCurrentTime(LocalDateTime.now(ZoneId.systemDefault()));
        return result;
    }

    /**
     * 生成失败 BaseResponse
     *
     * @param code
     * @param errorMessage
     * @return
     */
    public static BaseResponse error(Integer code, String errorMessage) {
        BaseResponse result = new BaseResponse();
        result.setSuccess(false);
        result.setCode(code);
        result.setErrorMessage(errorMessage);
        result.setCurrentTime(LocalDateTime.now());
        return result;
    }

    /**
     * 生成成功 RestResponse
     *
     * @param result
     * @param data
     * @return
     */
    public static RestResponse success(RestResponse result, Object data) {
        if (null == result) {
            result = new RestResponse();
        }
        result.setSuccess(true);
        result.setCode(CommonConstant.SUCCESS);
        result.setErrorMessage("success");
        result.setCurrentTime(LocalDateTime.now());
        if (ValidateUtils.isNotNullOrEmpty(data)) {
            Long lineNumber;
            if (data instanceof Page) {
                //如果是Page类型，计算行号并赋给返回值
                Page page = (Page) data;
                Long currentPage = page.getCurrent();
                Long size = page.getSize();
                List<BaseViewVo> records = page.getRecords();
                if (CollectionUtils.isNotEmpty(records)) {
                    int i = 1;
                    for (BaseViewVo baseViewVo : records) {
                        lineNumber = size * (currentPage - 1) + i++;
                        baseViewVo.setLineNumber(lineNumber);
                    }
                }
            }
//            else if (data instanceof List) {
                //如果是List类型，遍历结果并赋值行号
//                List<Object> list = (List) data;
//                lineNumber = 1L;
//                if (CollectionUtils.isNotEmpty(list)) {
//                    for (Object tempObject : list) {
//                        if (tempObject instanceof BaseViewVo){
//                            tempObject = (BaseViewVo)tempObject;
//                            tempObject.setLineNumber(lineNumber++);
//                        }
//
//                    }
//                }
//            }
        }
        result.setData(data);
        return result;
    }

    /**
     * 生成失败 RestResponse
     *
     * @param code
     * @param errorMessage
     * @return
     */
    public static RestResponse error(RestResponse result, Integer code, String errorMessage) {
        if (null == result) {
            result = new RestResponse();
        }
        result.setSuccess(false);
        result.setCode(code);
        result.setErrorMessage(errorMessage);
        result.setCurrentTime(LocalDateTime.now());
        return result;
    }

}
